import videoApi from '@videoApi/video'
import {validatenull} from '@/libs/validate'

export const videoMixin = {
  data() {
    return {
      // 视频点集
      videoMarkers: []
    }
  },
  methods: {
    /**
     * @Author ling.yuan@topevery.club
     * @Date 2019年12月10日 09:56:30
     * @Param event 是否开启视频图层(true/false)
     * @Description 加载视频数据并添加至地图
     */
    initVideoFeature(event, target = 'video') {
      if (event) {
        const query = {
          page: false
        }
        videoApi.list(query)
          .then(res => {
            // 将视频加载到地图上
            this.videoMarkers = []

            if (res.data.length > 0) {
              res.data.forEach(item => {
                // 坐标有效
                if (!validatenull(item.lat) && !validatenull(item.lng)) {
                  const obj = {
                    id: item.id,
                    title: item.name,
                    target: target,
                    coordinates: [item.lat, item.lng],
                    animate: true,
                    stopEvent: true,
                    data: item
                  }
                  this.videoMarkers.push(obj)
                }
              })
              this.showMarkerFeature(this.videoMarkers, false, target)
            } else {
              this.$message.warning('暂无视频监控数据')
            }
          })
          .catch(() => {

          })
      } else {
        // 清除图层feature
        if (this.videoMarkers.length > 0) {
          this.clearMarkerFeature(target)
          this.$refs.videoBar.handleCloseVideoDetail()
        }
      }
    },
    /**
     * @Author ling.yuan@topevery.club
     * @Date 2019/12/09 14:48:39
     * @Description 视频点击展示详情
     */
    handleVideoClick(event) {
      this.$refs.videoBar.handleShowVideoDetail(event)
      this.$refs.caseBar.handleCloseCaseInfo()
      this.$refs.staffBar.handleStaffAllHide()
      this.$refs.carBar.handleCloseCarDetail()
    },
    /**
     * @Author ling.yuan@topevery.club
     * @Date 2019/12/12 14:20:06
     * @Description 视频定位
     */
    handleVideoPosition(item, target = 'video') {
      for (let i = 0; i < this.videoMarkers.length; i++) {
        if (this.videoMarkers[i].id === item.id) {
          this.handleItemSelect(this.videoMarkers[i], false, target, false)
          break
        }
      }
    }
  }
}
